<?php
/**
 * Description of TabelaViewGenerator
 *
 * @author Magno
 */
class TabelaViewGenerator extends ViewGenerator{

    public function __construct($tableMeta, $viewParams) {
        parent::__construct($tableMeta, $viewParams, Constantes::$TEMPLATES_VIEW_DIR."tabela.bloum");
    }

    public function generate() {
        parent::generateNames();
        $this->generatePkUrlParam();
        $this->generateAttValues();
        return $this->templateView;
    }

    public function generatePkUrlParam(){
        $tableName = $this->tableMeta->getName();
        $pkUrlParam = "";

        $attrsKeys = array_keys($this->attPK);

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $this->attPK[$attrsKeys[$i]];
            $name = $att->getName();

            $pkUrlParam .= $name.'={$'.$tableName."->get".ucfirst($name).'()}';

            if($i < ( count($this->attPK)-1 )){
                $pkUrlParam .= "&";
            }
        }

        $this->templateView = str_replace(Constantes::$TPL_VIEW_PK_URL_PARAM, $pkUrlParam, $this->templateView);
    }

    public function generateAttValues(){
        $this->generateAttValuesParam($this->tableMeta, $this->tableMeta->getName());
    }

    public function generateAttValuesParam($table, $tableName){
        
        $attributes = $table->getAttributes();
        $attrsKeys = array_keys($attributes);

        $attHeadTable = "";
        $attRowTable = "";

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $attributes[$attrsKeys[$i]];
            $name = $att->getName();

            if($att->isPrimaryKey() && !$att->isForeignKey())  continue;

            if(!$att->isForeignKey()){
                $attHeadTable .= '<th scope="col">'.ucfirst($name).'</th>';
                $attRowTable .= '<td>{$'.$tableName.'->get'.ucfirst($name).'()}</td>';
            }
            else{
                $tbFkName = str_replace("ID", "", $name);
                $fks = $table->getFKReferences();
                $fkMeta = null;
                foreach ($fks as $fk) {
                    foreach ($fk->getAttributes() as $fkAtt) {
                        if(strcmp(strtolower($fkAtt->getName()), strtolower($tbFkName."ID")) == 0){
                            $fkMeta = $fk;
                            break;
                        }
                    }
                    if($fkMeta != null) break;
                }
                if($fkMeta == null)
                    throw new Exception ("Tabela (".$tbFkName.") Referenciada Na Herança com (".$this->tableMeta->getName().") Não Encontrada!!!");

                if($fkMeta->isExtends())
                    $this->generateAttValuesParam ($fkMeta->getTableReference(), $tableName);

                $tbReference = $fkMeta->getTableReference()->getName();
                $tab = "\n";
                if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_COMBO) == 0){
                    $comboDisplay = "";
                    foreach ($fk->getTableReference()->getAttributes() as $attFk) {
                        if($attFk->isComboDisplay()){
                            $comboDisplay = ucfirst($attFk->getName());
                            break;
                        }
                    }

                    $attHeadTable .= '<th scope="col">'.ucfirst($tbReference).'</th>';
                    $attRowTable .= '<td>{$'.$tableName.'->get'.ucfirst($tbReference).'()->get'.$comboDisplay.'()}</td>';
                }
            }

            $attHeadTable .= "\n\t";
            $attRowTable .= "\n\t\t";

        }

        $this->templateView = str_replace(Constantes::$TPL_VIEW_LISTAR_ATT_HEAD_TABLE, $attHeadTable, $this->templateView);
        $this->templateView = str_replace(Constantes::$TPL_VIEW_LISTAR_ATT_ROW_TABLE, $attRowTable, $this->templateView);

    }

    public function generateFile($generate = true){
        if($generate)
            $this->generate();
        parent::generateFile("tabela");
    }
}
?>
